
// uuid 一般项目中都已经存在了,如果没有安装即可
// https://github.com/uuidjs/uuid
import { v4 as uuidv4 } from 'uuid';

let TOKEN_KEY = "TOKEN_KEY"

/*
  这个函数是让用户获取到唯一的标识(一个浏览器只对应一个标识)
  先从 localStorage 当中去获取值,如果没有再调用 uuid 创建新的,并且还要存储到 localStorage
*/
function getUserTempId () {
  let userTempId = localStorage.getItem('USERTEMPID_KEY')
  if (!userTempId) {
    userTempId = uuidv4();
    localStorage.setItem('USERTEMPID_KEY', userTempId)
  }
  return userTempId
}

function setToken (token) {
  localStorage.setItem(TOKEN_KEY, token)
}

function getToken () {
  return localStorage.getItem(TOKEN_KEY)
}

function removeToken () {
  localStorage.removeItem(TOKEN_KEY)
}


export { getUserTempId, setToken, getToken, removeToken }